from django.db import models
from rbac.models import Role,WorkFlowModel,PositionModel,UserModel
# Create your models here.
class PowerWorkFlowModel(models.Model):
    role = models.ManyToManyField(Role,related_name='pwf_role',blank=True,db_constraint=False,verbose_name="角色")
    workflow = models.ForeignKey(WorkFlowModel,related_name='pwf_workflow',blank=True,null=True,
                                 db_constraint=False,verbose_name="工作流",on_delete=models.CASCADE)

    def __str__(self):
        return self.workflow.name

    class Meta:
        verbose_name = "权限工作流"
        verbose_name_plural = verbose_name


class EAWorkFlowModel(models.Model):
    workflow = models.ForeignKey(WorkFlowModel,related_name='eawf_workflow',blank=True,null=True,
                                 db_constraint=False,verbose_name="工作流",on_delete=models.CASCADE)
    position = models.ForeignKey(PositionModel,related_name='eawf_position',blank=True,null=True,
                                 db_constraint=False,verbose_name="职位",on_delete=models.CASCADE)
    level = models.IntegerField(verbose_name="步骤")

    def __str__(self):
        return self.workflow.name

    class Meta:
        verbose_name = "工作流审批"
        verbose_name_plural = verbose_name



class TaskWorkFlowModel(models.Model):
    TASK_STATUS_CHOICES = (
        (1,"新建"),
        (2,"审批中"),
        (3,"完成"),
    )
    name = models.CharField(max_length=128,verbose_name="任务名称")
    params = models.TextField(verbose_name="参数")
    workflow = models.ForeignKey(WorkFlowModel,related_name='twf_workflow',blank=True,null=True,
                                db_constraint=False,verbose_name="工作流",on_delete=models.CASCADE)
    user = models.ForeignKey(UserModel,related_name='twf_user',blank=True,null=True,
                             db_constraint=False,on_delete=models.CASCADE,verbose_name="用户")
    next_audit = models.IntegerField(verbose_name="下一步审批人id")
    task_status = models.SmallIntegerField(choices=TASK_STATUS_CHOICES,verbose_name="任务状态")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "任务工作流"
        verbose_name_plural = verbose_name




class RecordWorkFlowModel(models.Model):
    STATUS_CHOICES = (
        (0,"拒绝"),
        (1,"同意"),
        )
    task = models.ForeignKey(TaskWorkFlowModel,related_name='rwf_task',
                            blank=True,null=True,db_constraint=False,on_delete=models.CASCADE,verbose_name="任务")
    audit_user = models.IntegerField(verbose_name="审批人id")
    audit_time = models.DateTimeField(auto_now_add=True,verbose_name="审批时间")
    audit_status = models.SmallIntegerField(choices=STATUS_CHOICES,verbose_name="审批状态")

    def __str__(self):
        return self.task.name

    class Meta:
        verbose_name = "记录工作流"
        verbose_name_plural = verbose_name
